using System;
using System.Collections.Generic;
using System.Text;
using SQLGen;
using Microsoft.FSharp.Core;

namespace SQLGenerationTest
{
    class TestInsert
    {
        public static bool PerformTest()
        {
            string sql = "";
            RunTest[] tests = { InsertValues, BasicInsertSelect, InsertFromSelect, FullInsertSelect };
            foreach (RunTest test in tests)
            {
                sql = test();
                Console.WriteLine(sql + "\n");
                Log.Write(sql + "\n");
            }

            return true;
        }

        /*
         * To build out:
    		* InsertVals string * (string * string) array
    		* insertSel fields * table * from * where
    		* insertFromSel fields * table * selectStatement
    		* InsertSel (string * string) array * string * Q * From * Where
         * * */

        static string InsertValues()
        {
            Statement stmt = Statement.InsertVals(
                Table.Name("mytable"),                                                                  // table
                new Tuple<string, string>[] {                                               // field values
                    new Tuple<string, string>("foo", "'xyzzy'"),
                    new Tuple<string, string>("bar", "42"),
                    new Tuple<string, string>("baz", "'11/14/07'") } );

            return SqlModel.emitStatementSql(stmt);
        }

        static string BasicInsertSelect()
        {
            Statement stmt = Statement.InsertSel(
                new Tuple<string, string>[] {                                               // field values
                    new Tuple<string, string>("foo", "'plugh'"),
                    new Tuple<string, string>("bar", "bar"),
                    new Tuple<string, string>("baz", "baz"),
                    new Tuple<string, string>("joinfield", "joinfield") },
                Table.Name("mytable"),                                                                  // insert table name
                From.TableName("mytable").InnerJoin("anothertable", "joinfield", "joinfield"),  // from/join
                Where.Fld("foo").EQStr("xyzzy"));

            return SqlModel.emitStatementSql(stmt);
        }

        static string InsertFromSelect()
        {
            Statement stmt = Statement.InsertFromSel(
                new string[] { "foo", "bar", "baz" },                                       // fields
                Table.Name("mytable"),
                Statement.SimpleSel(
                    new string[] { "foo", "bar", "baz" },                                   // fields
                    From.TableName("mytable").InnerJoin("anothertable", "joinfield", "joinfield"),      // from/join
                    Where.All));                                                            // where clause

            return SqlModel.emitStatementSql(stmt);
        }

        static string FullInsertSelect()
        {
            Statement stmt = Statement.InsertSel(
                new Tuple<string, Field>[] {                                               // field values
                    new Tuple<string, Field>("foo", Field.Calc("'plugh'")),
                    new Tuple<string, Field>("bar", Field.Name("bar")),
                    new Tuple<string, Field>("baz", Field.Name("baz")),
                    new Tuple<string, Field>("joinfield", Field.Name("joinfield")) },
                Table.Name("mytable"),                                                                  // insert table name
                Quantifier.TopPercent(5),                                                   // quantifier
                From.TableName("mytable").InnerJoin("anothertable", "joinfield", "joinfield"),  // from/join
                Where.Fld("foo").EQStr("xyzzy"));


            return SqlModel.emitStatementSql(stmt);
        }
    }
}
